<template>
  <div class="cart">

    <section class="cart_warp" v-if="carList.length > 0">
      <van-cell-group>
        <van-cell style="font-weight: bold;" title="全场包邮" isLink>
          <router-link style="font-weight: 100;font-size: 14px; color: #999;" to="/category" class="order_all">再逛逛</router-link>
        </van-cell>
      </van-cell-group>


      <ul class="goods-wrap">
        <li v-for="item in carList" class="carListItem" :key="item.productId">
          <div class="cart-one-item">
            <!--  <input type="checkbox"  :checked="item.isChecked" :id="item.id" @change="chooseChange(item.id)"> -->

            <van-checkbox
              :checked-color="CFG.colorPrimary"
              style="padding-left: 10px;"
              :value="item.productId"
              v-model="item.selected"
              @click="chooseChange(item.productId)"
            ></van-checkbox>

            <div class="left"><img :src="item.image" /></div>
            <div class="right">
              <div class="text">
                <h3 class="title">{{ item.title }}</h3>
                <p class="desc"></p>
                <div>
                  <span class="price">
                    ￥
                    <span class="price-number">{{ item.sellPrice }}</span>
                  </span>
                </div>
              </div>
              <div class="cart-btn"><my-input-number :id="item.productId" :count="item.num" @changeNumberEvent="getOperator"></my-input-number></div>
            </div>
            <div class="dialog-box" @click.stop=""></div>
          </div>
        </li>
      </ul>
    </section>

    <section class="cart_empty" v-if="carList.length === 0">
      <div class="mb2"><i class="iconfont icon-gouwuche"></i></div>
      <span>购物车为空</span>
    </section>

    <section class="cart-footer" v-show="carList.length > 0">
      <div class="quanxuan"><van-checkbox :checked-color="CFG.colorPrimary" :value="allSelsect" @click="checkAll">全选</van-checkbox></div>
      <div class="heji">
        合计：
        <span>￥{{ (this.allMoney / 1).toFixed(2) }}</span>

        <button @click="toOrderForm" :disabled="checkedNum === 0" class="submit">去结算</button>

      </div>
    </section>

    <app-nav></app-nav>
  </div>
</template>

<script>
import { Checkbox, CheckboxGroup } from 'vant';
import { Dialog } from 'vant';
import nav from './nav.vue';

import { mapActions, mapGetters } from 'vuex';

export default {
  components: {
    appNav: nav,
    [Checkbox.name]: Checkbox,
    [CheckboxGroup.name]: CheckboxGroup
  },

  data() {
    return {
    };
  },


  created() {
    this.updateAllCheck();
  },

  computed: {
    // selectedData(){
    //   return this.$store.state.selectedData;
    // },

    ...mapGetters(['carList', 'allMoney', 'allSelsect']),



    isChecked() {
      return id => {
        return this.inArray(id, this.selectedData);
      };
    },

    counter() {
      // let that = this;

      return function(id) {
        let result = 0;
        this.carList.some(good => {
          if (good.productId === id) {
            result = good.num;
          }
        });

        //   console.log(result);
        return result;
      };
    },

    checkedNum() {
      let i = 0;
      this.carList.forEach((item, index) => {
        if (item.selected === true) {
          i++;
        }
      });
      return i;
    }
  },

  methods: {
    ...mapActions(['addGoods', 'addNum', 'jianNum', 'deleteGoodsFromCart', 'cartSelect', 'cartSelectAll','clearSelected']),


test2(){

  this.clearSelected()
},
    inArray(val, arr) {
      return arr.some(function(v) {
        return val === v;
      });
    },

    updateAllCheck() {
      if (this.checkedNum == this.carList.length) {
        this.AllChecked = true;
      } else {
        this.AllChecked = false;
      }
    },
    remove(arr, val) {
      var index = arr.indexOf(val);
      if (index > -1) {
        arr.splice(index, 1);
      }
    },
    // 全选和反选
    checkAll() {

      this.cartSelectAll()

    },

    // 单选的change事件

    chooseChange(id) {

      this.cartSelect(id)

   //   this.$forceUpdate();
    //  this.updateAllCheck();
    },

    getOperator(op, id) {
      // console.log(this.counter(id));

      if (op === 'plus') {
        this.addNum(id);
        // this.$store.commit('addNum', id);
      } else {
        if (this.counter(id) === 1) {
          this.confirmDelete(id);
        } else {
          this.jianNum(id);
          //  this.$store.commit('jianNum', id);
        }
      }
    },

    confirmDelete(id) {
      Dialog.confirm({
        title: '删除确认',
        message: '你确定删除该商品么？'
      })
        .then(() => {
          // on confirm
          this.deleteGoodsFromCart(id);
          //this.$store.commit('deleteGoodsFromCart', id);
        })
        .catch(() => {
          // on cancel
        });
      // this.dialogVisible = false;
      // this.$store.commit('deleteGoodsFromCart', this.id);
    },

    toOrderForm() {
      if (this.carList.length > 0) {
        this.$router.push({
          path: '/checkout',
          query: {
            pay: this.amount
          }
        });
      }
    }
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {} //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>

<style lang="scss" scoped="scoped">
  @import "@/assets/scss/variable.scss";
.cart-one-item {
  height: 150px;
  width: 100%;
  font-size: 0;
  background-color: #fff;
  border-bottom: 1px solid #eee;
  display: table;
  > div {
    font-size: 13px;
    display: inline-block;
    vertical-align: middle;
    display: table-cell;
  }
}

.left {
  width: 40%;
  height: 100%;
  text-align: center;
}

.left > img {
  height: 70%;
}

.right {
  box-sizing: border-box;
  position: relative;
  height: 100%;
  width: 60%;
  text-align: left;
  padding-left: 10px;
  .text {
    position: absolute;
    line-height: 30px;
    top: 50%;
    transform: translateY(-50%);
  }
  .title {
  }
  .price {
    color: $colorAccent;
    .price-number {
      font-size: 18px;
      font-weight: 600;
    }
  }
  .cart-btn {
    position: absolute;
    bottom: 15px;
    right: 15px;
  }
}

.cart-footer {
  position: fixed;
  width: 100%;
  bottom: 60px;
  border-top: 1px solid #eee;
  left: 0;
  background-color: white;
  overflow: hidden;
  .quanxuan {
    float: left;
    margin-top: 15px;
    margin-left: 15px;
  }
  .heji {
    float: right;
    text-align: right;

    font-size: 14px;
    span {
      font-size: 16px;
      color: $colorAccent;
      font-weight: bold;
    }
    .submit {
      background-color: $colorPrimary;
      border: 0;
      font-size: 13px;
      padding: 15px 25px;
      color: white;
    }
  }
}

.goods-wrap {
  padding-bottom: 120px;
}
.cart_warp {
  margin-bottom: 120px;
}

.cart{
  height: 100%;
}

.cart_empty {
  height: 100%;
  display: flex;
  flex-flow: column;
  justify-content: center; /* 水平居中 */
  align-items: center; /* 垂直居中 */
  i {
    font-size: 50px;
    color: gray;
  }
  span {
    color: gray;
  }
}
</style>
